
package nixonftp.ui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JSplitPane;
import javax.swing.Timer;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/


public class NXSplitPane extends JSplitPane {
	private boolean reverse;

	private JComponent firstComponent;
	private JComponent secondComponent;

	private DividerAnimatorListener al;
	private Timer animator;

	private int prevLocation = -1;
	private int origDividerSize = 0;

	public NXSplitPane(int type, JComponent firstComponent, JComponent secondComponent) {
		super(type, firstComponent, secondComponent);
		this.firstComponent = firstComponent;
		this.secondComponent = secondComponent;
		al = new DividerAnimatorListener();
		animator = new Timer(10, al);
		origDividerSize = getDividerSize();
		if (secondComponent == null || firstComponent == null) setDividerSize(0);
		setContinuousLayout(true);
		setBorder(BorderFactory.createEmptyBorder());
	}

	public void setFirstComponent(JComponent firstComponent) {
		this.firstComponent = firstComponent;
	}

	public void setSecondComponent(JComponent secondComponent) {
		this.secondComponent = secondComponent;
	}

	public boolean toggle(boolean top) {
		Component c = (top) ? getTopComponent() : getBottomComponent();
		boolean goUp = c == null;
		reverse = goUp;

		if (prevLocation == -1) prevLocation = getHeight() / 2;

		if (animator.isRunning()) {
			animator.stop();
			al.forceFinished = true;
			al.actionPerformed(null);
			al.forceFinished = false;
		}

		if (goUp) setDividerSize(origDividerSize);
		
		if (top && goUp) {
			setTopComponent(firstComponent);
			setDividerLocation(getMinimumDividerLocation());
		} else if (goUp) {
			setBottomComponent(secondComponent);
			setDividerLocation(getMaximumDividerLocation());
		} else {
			prevLocation = getDividerLocation();
		}
		
		al.times = 0;
		al.top = top;
		animator.restart();
		
		return goUp;
	}

	class DividerAnimatorListener implements ActionListener {
		int times;
		boolean top;
		boolean forceFinished;
		
		public void actionPerformed(ActionEvent e) {
			times++;
			int location = getDividerLocation();
			int increment;
			int dec = times / 2;
			boolean finished;
			if (top) {
				if (reverse) { //sidebar expand
					increment = getWidth() / 20;
					finished = location + increment >= prevLocation || location >= getMaximumDividerLocation();
				} else {
					increment = -location / 10;
					if (increment > -1) increment = -1;
					finished = location + increment <= getMinimumDividerLocation();
				}
			} else {
				if (reverse) {
					increment = -location / 10;
					finished = location + increment <= prevLocation || location <= getMinimumDividerLocation();
				} else {
					increment = getHeight() / 20;
					if (increment < 1) increment = 1;
					finished = location + increment >= getMaximumDividerLocation();
				}
			}

			if (forceFinished) finished = true;
			if (!finished) {
				setDividerLocation(location + increment);
			} else {
				animator.stop();
				if (!reverse) {
					if (top) setTopComponent(null);
					else setBottomComponent(null);
					setDividerSize(0);
				} else {
					setDividerLocation(prevLocation);
				}
			}
		}
	}
}
